var merchantsTable;
var merchantsStates =
{
	'0' : '未提交',
	'1' : '审核中',
	'2' : '审核通过',
	'3' : '审核驳回',
	'4' : '下架'
};
/****列表信息*/
function showMerchants()
{
	showLoading("查询中...");
	$.ajax(
	{
		url : 'merchants/listPage.do',
		type : 'get',
		dataType : 'html',
		async : true,
		success : function(html)
		{
			$('#mainForm').html(html);
			queryMerchantsDatas();
			hideLoading();
			//绑定查询按钮 
		$('#searchMerchantBtn').click(function()
		{
			queryMerchantsPage();
		});
	}
	});
}
function queryMerchantsPage()
{
	showLoading("查询中...");
	queryMerchantsDatas();
	hideLoading();
}
function queryMerchantsDatas()
{
	merchantsTable = $('#entTable').table(
			{
				width : '100%',
				defaultOrderBy : "sort",
				defaultOrderType : "asc",
				form :
				{
					url : 'merchants/queryList.do',
					id : '#entSearchForm',
					type : 'post',
					pageNumberText : '#pageNum'
				},
				pageable : true,
				selectable : true,
				colums : [
						{
							columnName : '商家名称',
							name : 'name',
							handle : "toViewMerchants"
						},
						{
							columnName : '商家编号',
							name : 'no'
						},
						{
							columnName : '联系人',
							name : 'lxr'
						},
						{
							columnName : '联系电话',
							name : 'lxrTel'
						},
						{
							columnName : '开始日期',
							name : 'actiBeginTime'
						},
						{
							columnName : '结束日期',
							name : 'actiEndTime'
						},
						{
							columnName : '状态',
							name : 'status',
							sortable : false,
							formatter : function(obj)
							{
								return merchantsStates[obj.status];
							}
						},
						{
							columnName : '操作',
							name : 'status',
							sortable : false,
							formatter : function(obj)
							{
							
								if (obj.status == "0" || obj.status == "3" || obj.status == "4")
								{
								return '<input type="button" value="查看"   style="cursor: pointer" onclick="toViewMerchants(' + obj.id
										+ ')"><input type="button" value="删除"   style="cursor: pointer" onclick="toDelMerchantById(' + obj.id + ')">';
								}
								else if (obj.status == "1")
								{
								return '<input type="button" value="操作"   style="cursor: pointer" onclick="toUpdateMerchantsState(' + obj.id
										+ ')"><input type="button" value="删除"   style="cursor: pointer" onclick="toDelMerchantById(' + obj.id + ')">';
								}
								else if (obj.status == "2")
								{
								return '<input type="button" value="操作"   style="cursor: pointer" onclick="toDown(' + obj.id
										+ ')"><input type="button" value="删除"   style="cursor: pointer" onclick="toDelMerchantById(' + obj.id + ')">';
								}
								else
								{
								return '<input type="button" value="查看"   style="cursor: pointer" onclick="toViewMerchants(' + obj.id
										+ ')"><input type="button" value="删除"   style="cursor: pointer" onclick="toDelMerchantById(' + obj.id + ')">';
								}
							}
						}]
			});
}
function toViewMerchants(id)
{
	var param = '?t=' + ts() + '&id=' + id;
	$.get('merchants/toView.do' + param, function(html)
	{
		$('#commonDialogDiv').html(html);
		$('#commonDialogDiv').dialog(
		{
			modal : true,
			title : '商家及活动信息查阅',
			width : 995,
			height : 600,
			buttons :
			{
				'关闭' : function()
				{
					$(this).dialog('close');
				}
			}
		});
	});
}
function toDown(id)
{
	var param = '?t=' + ts() + '&id=' + id;
	$.get('merchants/toUpdateDown.do' + param, function(html)
	{
		$('#commonDialogDiv').html(html);
		$('#commonDialogDiv').dialog(
		{
			modal : true,
			title : '商家及活动下架',
			width : 995,
			height : 600,
			buttons :
			{
				'下架' : function()
				{
					$("#model_status").val('4');
					saveMerchantDown();
				},
				'取消' : function()
				{
					$(this).dialog('close');
				}
			}
		});
		validator = $("#MerchantsForm").validate(
		{
			rules :
			{
				"model.xiajiaReason" :
				{
					maxlength : 150
				}
			},
			messages :
			{
				"model.xiajiaReason" :
				{
					maxlength : "最长不超过150字"
				}
			}
		});
	});
}
function toDelMerchantById(id)
{
	var ids = 'ids=' + id + "&t=" + (new Date());
	$('#commonDialogDiv').html('<p>确定要删除记录信息？</p>');
	$('#commonDialogDiv').dialog(
	{
		modal : true,
		width : 250,
		height : 160,
		buttons :
		{
			'确定' : function()
			{
				$(this).dialog('close');
				$.ajax(
				{
					url : 'merchants/batchUpdate.do?' + ids,
					type : 'get',
					dataType : 'json',
					success : function(data)
					{
						if (data.success)
						{
						queryMerchantsPage();
						}
						else
						{
						showDialog('删除失败，原因：' + data.message);
						}
					}
				});
			},
			'取消' : function()
			{
				$(this).dialog('close');
			}
		},
		close : function()
		{
			$(this).dialog('destroy');
		}
	});
}
function toUpdateMerchantsState(id)
{
	var param = '?t=' + ts() + '&id=' + id;
	$.get('merchants/toCheck.do' + param, function(html)
	{
		$('#commonDialogDiv').html(html);
		$('#commonDialogDiv').dialog(
		{
			modal : true,
			title : '商家及活动信息审核',
			width : 995,
			height : 600,
			buttons :
			{
				'保存' : function()
				{
					saveUpdateShenhe();
				},
				'通过' : function()
				{
					$("#model_status").val('2');
					saveUpdateShenhe();
				},
				'驳回' : function()
				{
					$("#model_status").val('3');
					saveUpdateShenhe();
				},
				'取消' : function()
				{
					$(this).dialog('close');
				}
			}
		});
		validator = $("#MerchantsForm").validate(
		{
			rules :
			{
				"model.detailInfo" :
				{
					maxlength : 150
				}
			},
			messages :
			{
				"model.detailInfo" :
				{
					maxlength : "最长不超过150字"
				}
			}
		});
	});
}
//保存审核信息
function saveUpdateShenhe()
{
	if (validator.form())
	{
	$('#MerchantsForm').ajaxSubmit(
	{
		url : 'merchants/saveCheckMerchants.do',
		type : 'post',
		dataType : 'json',
		success : function(data)
		{
			if (data.success)
			{
			showDialog('操作成功');
			$('#commonDialogDiv').dialog('close');
			queryMerchantsPage();
			}
			else
			{
			showDialog('保存失败，' + data.message);
			}
		}
	});
	}
}
//保存下架信息
function saveMerchantDown()
{
	if (validator.form())
	{
	$('#MerchantsForm').ajaxSubmit(
	{
		url : 'merchants/saveDownMerchants.do',
		type : 'post',
		dataType : 'json',
		success : function(data)
		{
			if (data.success)
			{
			showDialog('操作成功');
			$('#commonDialogDiv').dialog('close');
			queryMerchantsPage();
			}
			else
			{
			showDialog('保存失败，' + data.message);
			}
		}
	});
	}
}
//服务站操作的
function showMerchantsForFwz()
{
	showLoading("查询中...");
	$.ajax(
	{
		url : 'merchants/listPage.do',
		type : 'get',
		dataType : 'html',
		async : true,
		success : function(html)
		{
			$('#mainForm').html(html);
			queryMerchantsDatas2();
			hideLoading();
			//绑定查询按钮 
		$('#searchMerchantBtn').click(function()
		{
			queryMerchantsPage2();
		});
	}
	});
}
function queryMerchantsPage2()
{
	showLoading("查询中...");
	queryMerchantsDatas2();
	hideLoading();
}
function queryMerchantsDatas2()
{
	merchantsTable = $('#entTable').table(
			{
				width : '100%',
				defaultOrderBy : "sort",
				defaultOrderType : "asc",
				form :
				{
					url : 'merchants/queryList.do',
					id : '#entSearchForm',
					type : 'post',
					pageNumberText : '#pageNum'
				},
				pageable : true,
				selectable : true,
				colums : [
						{
							columnName : '商家名称',
							name : 'name',
							handle : "toViewMerchants"
						},
						{
							columnName : '商家编号',
							name : 'no'
						},
						{
							columnName : '联系人',
							name : 'lxr'
						},
						{
							columnName : '联系电话',
							name : 'lxrTel'
						},
						{
							columnName : '开始日期',
							name : 'actiBeginTime'
						},
						{
							columnName : '结束日期',
							name : 'actiEndTime'
						},
						{
							columnName : '状态',
							name : 'status',
							sortable : false,
							formatter : function(obj)
							{
								return merchantsStates[obj.status];
							}
						},
						{
							columnName : '操作',
							name : 'status',
							sortable : false,
							formatter : function(obj)
							{
								if (obj.status == "0" || obj.status == "3" || obj.status == "4")
								{
								return '<input type="button" value="编辑"   style="cursor: pointer" onclick="toAddMerchants(' + obj.id
										+ ')"><input type="button" value="查看"   style="cursor: pointer" onclick="toViewMerchants(' + obj.id + ')">';
								}
								else if (obj.status == "1")
								{
								return '<input type="button" value="查看"   style="cursor: pointer" onclick="toViewMerchants(' + obj.id + ')">';
								}
								else if (obj.status == "2")
								{
								return '<input type="button" value="查看"   style="cursor: pointer" onclick="toViewMerchants(' + obj.id + ')">';
								}
								else
								{
								return '<input type="button" value="查看"   style="cursor: pointer" onclick="toViewMerchants(' + obj.id + ')">';
								}
							}
						}]
			});
}
//添加
function toAddMerchants(id)
{
	var param = '?t=' + ts();
	if (id)
	{
	param = '?id=' + id + '&t=' + ts();
	}
	$.get('merchants/toEditMerchants.do' + param, function(html)
	{
		$('#commonDialogDiv').html(html);
		addValidatorFloat();
		addValidatorInteger();
		addValidatorAllPhone();
		$('#commonDialogDiv').dialog(
		{
			modal : true,
			title : '商家及活动信息',
			width : 995,
			height : 600,
			buttons :
			{
				'提交' : function()
				{
					$("#model_status").val(1);
					saveMerchants();
				},
				'保存' : function()
				{
					//$("#model_status").val(0);
				saveMerchants();
			},
			'取消' : function()
			{
				$(this).dialog('close');
			}
			}
		});
		initFormToDatepicker("queryStartDate", "queryEndDate", "", "");
		var _lat = $("#modelLatitude").val();
		var _lng = $("#modelLongitude").val();
		if (_lat && _lng)
		{
		}
		else
		{
		_lat = 39.90388637128857;
		_lng = 116.40758371300763;
		}
		//高德地图
			map = new AMap.Map("map",
			{
				center : new AMap.LngLat(_lng, _lat),
				level : 17
			}); // 创建Map实例
			map.plugin(["AMap.ToolBar"], function()
			{
				toolBar = new AMap.ToolBar();
				map.addControl(toolBar);
			});
			loadMapbyLatLng(_lng, _lat);
			validator = $("#MerchantsForm").validate(
			{
				rules :
				{
					"model.name" :
					{
						required : true,
						maxlength : 20
					},
					"model.no" :
					{
						required : true,
						integer : true,
						minlength : 8,
						maxlength : 8
					},
					"model.lxr" :
					{
						required : true
					},
					"model.lxrTel" :
					{
						required : true,
						allPhone : true
					},
					"model.allowTime" :
					{
						maxlength : 500
					},
					"model.useRules" :
					{
						maxlength : 500
					},
					"model.invaliDates" :
					{
						maxlength : 500
					},
					"model.detailInfo" :
					{
						maxlength : 150
					}
				},
				messages :
				{
					"model.name" :
					{
						required : "必填"
					},
					"model.no" :
					{
						required : "必填",
						integer : "数字",
						minlength : "8位数字",
						maxlength : "8位数字"
					},
					"model.lxr" :
					{
						required : "必填"
					},
					"model.lxrTel" :
					{
						required : "必填"
					},
					"model.allowTime" :
					{
						maxlength : "最大长度500字"
					},
					"model.useRules" :
					{
						maxlength : "最大长度500字"
					},
					"model.invaliDates" :
					{
						maxlength : "最大长度500字"
					},
					"model.detailInfo" :
					{
						maxlength : "最长不超过150字"
					}
				}
			});
		});
}

function saveMerchants()
{
	if (validator.form())
	{
	$('#MerchantsForm').ajaxSubmit(
	{
		url : 'merchants/saveMerchants.do',
		type : 'post',
		dataType : 'json',
		success : function(data)
		{
			if (data.success)
			{
			showDialog('操作成功');
			$('#commonDialogDiv').dialog('close');
			queryMerchantsPage2();
			}
			else
			{
			showDialog('保存失败，' + data.message);
			}
		}
	});
	}
}